#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "hi_io.h"
#define LED_TASK_GPIO 6                         
#define LED_INTERVAL_TIME 30
#define LED_TASK_STACK_SIZE 512
#define LED_TASK_PRIO 25
enum LedState {
     LED_ON = 0,
     LED_OFF,
     LED_SPARK,
};
enum LedState g_ledState = LED_SPARK;
static void *LedTask(const char *arg)
{
 (void)arg;
 
 while (1) {
    switch (g_ledState) {
    case LED_ON:
    //添加 GPIP 控制灯开
    IoTGpioSetOutputVal(LED_TASK_GPIO,0);
    osDelay(LED_INTERVAL_TIME);
    break;
    case LED_OFF:
    //添加 GPIP 控制灯关
    IoTGpioSetOutputVal(LED_TASK_GPIO,1);
    osDelay(LED_INTERVAL_TIME);
    break;
    case LED_SPARK:
    //添加 GPIP 控制灯闪
    IoTGpioSetOutputVal(LED_TASK_GPIO,0);
    osDelay(LED_INTERVAL_TIME);
    IoTGpioSetOutputVal(LED_TASK_GPIO,1);
    osDelay(LED_INTERVAL_TIME);
    break;
    default:
    osDelay(LED_INTERVAL_TIME);
    break;
    }
 }
 return NULL;
}
static void LedExampleEntry(void)
{
    osThreadAttr_t attr;
    /*LED 的 GPIO 初始化 */
    IoTGpioInit(LED_TASK_GPIO);

    IoTGpioSetFunc(LED_TASK_GPIO, HI_IO_FUNC_GPIO_6_GPIO);
    /*GPIO 设置为输出 */
    IoTGpioSetDir(LED_TASK_GPIO,IOT_GPIO_DIR_OUT);
    attr.name = "LedTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = LED_TASK_STACK_SIZE;
    attr.priority = LED_TASK_PRIO;
    if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
    printf("[LedExample] Falied to create LedTask!\n");
    }
    
}
SYS_RUN(LedExampleEntry);
